/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml.gui;

import java.awt.Color;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rtextarea.RTextScrollPane;
import org.xml.sax.SAXException;
import xml.excepciones.OpcionDeProcesamientoException;
import xml.modelo.ArchivoXML;
import xml.modelo.Atributo;
import xml.modelo.CellRender;
import xml.modelo.Tag;
import xml.util.Abrir;
import xml.util.Archivo;
import xml.util.Cambiar;
import xml.util.Guardar;
import xml.util.Mensaje;
import xml.util.Temas;
import xml.util.TextUtil;
import acercade.*;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author Administrador
 */
public class Aplicacion extends javax.swing.JFrame implements KeyListener, DropTargetListener{
    private RSyntaxTextArea area = new RSyntaxTextArea();
    private File archivo;
    private Tag tagRaiz;
    private final String  NOMBRE_SW;
    private Object[] forms = new Object[3];
    /**
     * Creates new form Aplicacion
     */
    public Aplicacion() throws ParserConfigurationException, SAXException, IOException, OpcionDeProcesamientoException {
        NOMBRE_SW = "JXML Viewer";
        initComponents();
        arbol.setRootVisible(false);
        this.setBounds(0,0,800,600);
        crearTextArea();
        this.setLocationRelativeTo(null);
        archivo = null;
        tagRaiz = null;
        Tag a = new Tag("raiz", "");
        a.addTagHijo(new Tag("wea","hola"));
        try {
            xml.analizador.XML.crearArchivoXML(a, new File("prueba.xml"));
        } catch (TransformerConfigurationException ex) {
            ex.printStackTrace();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (TransformerException ex) {
            ex.printStackTrace();
        }
        arbol.setBackground(new Color(240,255,255));
        arbolTags.setBackground(new Color(240,255,255));
        arbolTexto.setBackground(new Color(240,255,255));
        new TextUtil(tagText, "Crea un Tag acá");
        Cambiar.iconoDeFormulario(this, "/xml/images/16cerrado.png");
        diaText.setIconImage(this.getIconImage());
        dialAtr.setIconImage(this.getIconImage());
        
        forms[0] = this;
        forms[1] = dialAtr;
        forms[2] = diaText;
        Temas.cambiarTema(Temas.SO_ACTUAL, forms);
        
        initAcercaDe();
    }

     private void expandAll(JTree tree) {
        int row = 0;
        while (row < tree.getRowCount()) {
            tree.expandRow(row);
            row++;
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dialAtr = new javax.swing.JDialog();
        jLabel2 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        nombre = new javax.swing.JTextField();
        valor = new javax.swing.JTextField();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        arbolTags = new javax.swing.JTree();
        diaText = new javax.swing.JDialog();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        texto = new javax.swing.JTextField();
        jButton11 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        arbolTexto = new javax.swing.JTree();
        buttonGroup1 = new javax.swing.ButtonGroup();
        chos = new javax.swing.JFileChooser();
        jSplitPane2 = new javax.swing.JSplitPane();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        arbol = new javax.swing.JTree();
        panelText = new javax.swing.JPanel();
        jToolBar2 = new javax.swing.JToolBar();
        tagVacio = new javax.swing.JCheckBox();
        tagText = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jButton6 = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        jButton7 = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        jButton8 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        jButton2 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        opAuto = new javax.swing.JCheckBox();
        jButton5 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItem2 = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItem3 = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItem4 = new javax.swing.JRadioButtonMenuItem();
        jMenu4 = new javax.swing.JMenu();
        menuItemAcercaDe = new javax.swing.JMenuItem();

        dialAtr.setTitle("Atributo");

        jLabel2.setText("Tags:");

        jLabel6.setText("Nombre:");

        jLabel7.setText("Valor:");

        jButton9.setText("Aceptar");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        jButton10.setText("Cancelar");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        arbolTags.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jScrollPane2.setViewportView(arbolTags);

        javax.swing.GroupLayout dialAtrLayout = new javax.swing.GroupLayout(dialAtr.getContentPane());
        dialAtr.getContentPane().setLayout(dialAtrLayout);
        dialAtrLayout.setHorizontalGroup(
            dialAtrLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dialAtrLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(dialAtrLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(dialAtrLayout.createSequentialGroup()
                        .addGroup(dialAtrLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addGap(18, 18, 18)
                        .addGroup(dialAtrLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                            .addComponent(nombre, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                            .addComponent(valor, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, dialAtrLayout.createSequentialGroup()
                        .addComponent(jButton9, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)))
                .addContainerGap())
        );
        dialAtrLayout.setVerticalGroup(
            dialAtrLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dialAtrLayout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(dialAtrLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(dialAtrLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(dialAtrLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(valor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(dialAtrLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(33, 33, 33))
        );

        diaText.setTitle("Texto");

        jLabel8.setText("Tags:");

        jLabel9.setText("Texto:");

        jButton11.setText("Aceptar");
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });

        jButton12.setText("Cancelar");
        jButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton12ActionPerformed(evt);
            }
        });

        treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        arbolTexto.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jScrollPane3.setViewportView(arbolTexto);

        javax.swing.GroupLayout diaTextLayout = new javax.swing.GroupLayout(diaText.getContentPane());
        diaText.getContentPane().setLayout(diaTextLayout);
        diaTextLayout.setHorizontalGroup(
            diaTextLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(diaTextLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(diaTextLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addGroup(diaTextLayout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addGap(18, 18, 18)
                        .addGroup(diaTextLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
                            .addComponent(texto, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, diaTextLayout.createSequentialGroup()
                        .addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)))
                .addContainerGap())
        );
        diaTextLayout.setVerticalGroup(
            diaTextLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(diaTextLayout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(diaTextLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(diaTextLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(texto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(diaTextLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(NOMBRE_SW + " - Archivo NO guardado");
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        jSplitPane2.setDividerLocation(300);
        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane2.setOneTouchExpandable(true);

        jSplitPane1.setDividerLocation(300);
        jSplitPane1.setOneTouchExpandable(true);

        treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        arbol.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        arbol.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                arbolValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(arbol);

        jSplitPane1.setLeftComponent(jScrollPane1);

        panelText.setLayout(new java.awt.BorderLayout());

        jToolBar2.setFloatable(false);
        jToolBar2.setRollover(true);

        tagVacio.setText("tagVacio");
        tagVacio.setFocusable(false);
        tagVacio.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        tagVacio.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar2.add(tagVacio);

        tagText.setFont(new java.awt.Font("Tahoma", 2, 12));
        tagText.setForeground(new java.awt.Color(153, 153, 153));
        tagText.setText("Crea un Tag acá");
        tagText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tagTextKeyReleased(evt);
            }
        });
        jToolBar2.add(tagText);
        jToolBar2.add(jSeparator1);

        jButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xml/images/16atributo.png"))); // NOI18N
        jButton6.setToolTipText("Crear atributo");
        jButton6.setFocusable(false);
        jButton6.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton6.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jToolBar2.add(jButton6);
        jToolBar2.add(jSeparator2);

        jButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xml/images/16comentario.png"))); // NOI18N
        jButton7.setToolTipText("Crear comentario");
        jButton7.setFocusable(false);
        jButton7.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton7.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jToolBar2.add(jButton7);
        jToolBar2.add(jSeparator3);

        jButton8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xml/images/16texto.png"))); // NOI18N
        jButton8.setToolTipText("Crear texto");
        jButton8.setFocusable(false);
        jButton8.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton8.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });
        jToolBar2.add(jButton8);

        panelText.add(jToolBar2, java.awt.BorderLayout.PAGE_START);

        jSplitPane1.setRightComponent(panelText);

        jSplitPane2.setTopComponent(jSplitPane1);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Leyenda"));
        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.PAGE_AXIS));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xml/images/16abierto.png"))); // NOI18N
        jLabel1.setText("Elemento o Tag");
        jPanel1.add(jLabel1);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xml/images/16atributo.png"))); // NOI18N
        jLabel3.setText("Atributo");
        jPanel1.add(jLabel3);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xml/images/16comentario.png"))); // NOI18N
        jLabel4.setText("Comentario");
        jPanel1.add(jLabel4);

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xml/images/16texto.png"))); // NOI18N
        jLabel5.setText("Texto o Contenido");
        jPanel1.add(jLabel5);

        jSplitPane2.setRightComponent(jPanel1);

        getContentPane().add(jSplitPane2, java.awt.BorderLayout.CENTER);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xml/images/abrir.png"))); // NOI18N
        jButton2.setText("Abrir XML");
        jButton2.setFocusable(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton2);

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xml/images/save.png"))); // NOI18N
        jButton4.setText("Guardar");
        jButton4.setFocusable(false);
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton4);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xml/images/16abierto.png"))); // NOI18N
        jButton1.setText("Expandir Todos");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xml/images/refresh.png"))); // NOI18N
        jButton3.setText("Refresh");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton3);

        opAuto.setSelected(true);
        opAuto.setText("Auto Refresh");
        opAuto.setFocusable(false);
        opAuto.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jToolBar1.add(opAuto);

        jButton5.setText("jButton5");
        jButton5.setFocusable(false);
        jButton5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton5.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton5);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.PAGE_START);

        jMenu1.setText("Archivo");

        jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xml/images/abrir.png"))); // NOI18N
        jMenuItem1.setText("Abrir XML");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xml/images/save.png"))); // NOI18N
        jMenuItem4.setText("Guardar");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Ver");

        jMenuItem2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xml/images/16abierto.png"))); // NOI18N
        jMenuItem2.setText("Expandir Todos");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuBar1.add(jMenu2);

        jMenu3.setText("Diseño");

        buttonGroup1.add(jRadioButtonMenuItem1);
        jRadioButtonMenuItem1.setSelected(true);
        jRadioButtonMenuItem1.setText("Sistema Operativo");
        jRadioButtonMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem1ActionPerformed(evt);
            }
        });
        jMenu3.add(jRadioButtonMenuItem1);

        buttonGroup1.add(jRadioButtonMenuItem2);
        jRadioButtonMenuItem2.setText("Java");
        jRadioButtonMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem2ActionPerformed(evt);
            }
        });
        jMenu3.add(jRadioButtonMenuItem2);

        buttonGroup1.add(jRadioButtonMenuItem3);
        jRadioButtonMenuItem3.setText("Nimbus");
        jRadioButtonMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem3ActionPerformed(evt);
            }
        });
        jMenu3.add(jRadioButtonMenuItem3);

        buttonGroup1.add(jRadioButtonMenuItem4);
        jRadioButtonMenuItem4.setText("Motif");
        jRadioButtonMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem4ActionPerformed(evt);
            }
        });
        jMenu3.add(jRadioButtonMenuItem4);

        jMenuBar1.add(jMenu3);

        jMenu4.setText("Ayuda");

        menuItemAcercaDe.setText("Acerca de...");
        jMenu4.add(menuItemAcercaDe);

        jMenuBar1.add(jMenu4);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        abrir();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        expandAll(arbol);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        abrir();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        refreshBoton();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        guardar();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        jSplitPane2.setDividerLocation(this.getHeight()-180);
        jSplitPane1.setDividerLocation(this.getWidth()/3);
    }//GEN-LAST:event_formComponentResized

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        if(arbol.getRowCount() != 0){
            try {
                ArchivoXML ar;
                if (archivo != null) {
                    ar = xml.analizador.XML.procesarArchivoXML(archivo, xml.analizador.XML.OPCION_COMPLEJA);
                } else {
                    ar = xml.analizador.XML.procesarArchivoXML(new File("temp.xml"), xml.analizador.XML.OPCION_COMPLEJA);
                }
                tagRaiz = ar.getTagRaiz();
                //aca se puede cambiar los iconos
                ImageIcon[] iconos = new ImageIcon[4];

                iconos[Tag.TAG] = CellRender.crearIcono("/xml/images/16abierto.png");
                iconos[Tag.COMENTARIO] = CellRender.crearIcono("/xml/images/16comentario.png");
                iconos[Tag.ATRIBUTO] = CellRender.crearIcono("/xml/images/16atributo.png");
                iconos[Tag.TEXTO] = CellRender.crearIcono("/xml/images/16texto.png");
                //aca se puede cambiar los iconos
                tagRaiz.construirArbolSoloTags(arbolTags, iconos, new Color(240,255,255));
                nombre.setText("");
                valor.setText("");
                arbolTags.setSelectionRow(0);
                dialAtr.setBounds(0, 0, dialAtr.getPreferredSize().width, dialAtr.getPreferredSize().height);
                dialAtr.setLocationRelativeTo(null);
                dialAtr.setVisible(true);
            } catch (OpcionDeProcesamientoException ex) {
                ex.printStackTrace();
            } catch (ParserConfigurationException ex) {
                ex.printStackTrace();
            } catch (SAXException ex) {
                Mensaje.error(ex.getMessage());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_jButton6ActionPerformed

    private void arbolValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_arbolValueChanged


    }//GEN-LAST:event_arbolValueChanged

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        DefaultMutableTreeNode m = (DefaultMutableTreeNode) arbolTags.getLastSelectedPathComponent();
        Object o = m.getUserObject();
        if(o instanceof Tag){
            Tag tagAbuscar = (Tag)o;
            Tag tag = Tag.getTagHijoRecursivo(tagRaiz, tagAbuscar);
            String nom, val;
            nom = nombre.getText().trim();
            val = valor.getText().trim();
            tag.addAtributo(new Atributo(nom, val));
            this.refreshInverso();
            dialAtr.setVisible(false);
        }
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        dialAtr.setVisible(false);
    }//GEN-LAST:event_jButton10ActionPerformed

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
    String nom = Mensaje.mostrarMensaje("Escriba del comentario", "");
    if(nom != null && !nom.trim().equalsIgnoreCase("")){
        area.getSelectionStart();
        area.insert("<!--"+nom+"-->", area.getSelectionStart());
        refresh();
    }
}//GEN-LAST:event_jButton7ActionPerformed

private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed
        DefaultMutableTreeNode m = (DefaultMutableTreeNode) arbolTexto.getLastSelectedPathComponent();
        Object o = m.getUserObject();
        if(o instanceof Tag){
            Tag tagAbuscar = (Tag)o;
            Tag tag = Tag.getTagHijoRecursivo(tagRaiz, tagAbuscar);
            String t = texto.getText().trim();
            tag.setValor(t);
            this.refreshInverso();
            diaText.setVisible(false);
        }
}//GEN-LAST:event_jButton11ActionPerformed

private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed
    diaText.setVisible(false);
}//GEN-LAST:event_jButton12ActionPerformed

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        if(arbol.getRowCount() != 0){
            try {
                ArchivoXML ar;
                if (archivo != null) {
                    ar = xml.analizador.XML.procesarArchivoXML(archivo, xml.analizador.XML.OPCION_COMPLEJA);
                } else {
                    ar = xml.analizador.XML.procesarArchivoXML(new File("temp.xml"), xml.analizador.XML.OPCION_COMPLEJA);
                }
                tagRaiz = ar.getTagRaiz();
                //aca se puede cambiar los iconos
                ImageIcon[] iconos = new ImageIcon[4];

                iconos[Tag.TAG] = CellRender.crearIcono("/xml/images/16abierto.png");
                iconos[Tag.COMENTARIO] = CellRender.crearIcono("/xml/images/16comentario.png");
                iconos[Tag.ATRIBUTO] = CellRender.crearIcono("/xml/images/16atributo.png");
                iconos[Tag.TEXTO] = CellRender.crearIcono("/xml/images/16texto.png");
                //aca se puede cambiar los iconos
                tagRaiz.construirArbolSoloTags(arbolTexto, iconos, new Color(240,255,255));
                texto.setText("");
                arbolTexto.setSelectionRow(0);
                diaText.setBounds(0, 0, diaText.getPreferredSize().width, diaText.getPreferredSize().height);
                diaText.setLocationRelativeTo(null);
                diaText.setVisible(true);
            } catch (OpcionDeProcesamientoException ex) {
                ex.printStackTrace();
            } catch (ParserConfigurationException ex) {
                ex.printStackTrace();
            } catch (SAXException ex) {
                Mensaje.error(ex.getMessage());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
}//GEN-LAST:event_jButton8ActionPerformed

private void tagTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tagTextKeyReleased
    if(evt.getKeyCode() == KeyEvent.VK_ENTER){
        String nom = tagText.getText().trim();
        tagText.setText("");
        if(!nom.equalsIgnoreCase("")){
            area.getSelectionStart();
            String tag = "";
            if(tagVacio.isSelected()){
                tag = "<"+nom+"/>";
            }else{
                tag = "<"+nom+"></"+nom+">";
            }
            area.insert(tag, area.getSelectionStart());
            refresh();
        }
    }
}//GEN-LAST:event_tagTextKeyReleased

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
    expandAll(arbol);
}//GEN-LAST:event_jMenuItem2ActionPerformed

private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
    guardar();
}//GEN-LAST:event_jMenuItem4ActionPerformed

private void jRadioButtonMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem1ActionPerformed
    Temas.cambiarTema(Temas.SO_ACTUAL, forms);
}//GEN-LAST:event_jRadioButtonMenuItem1ActionPerformed

private void jRadioButtonMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem2ActionPerformed
    Temas.cambiarTema(Temas.METAL, forms);
}//GEN-LAST:event_jRadioButtonMenuItem2ActionPerformed

private void jRadioButtonMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem3ActionPerformed
    Temas.cambiarTema(Temas.NIMBUS, forms);
}//GEN-LAST:event_jRadioButtonMenuItem3ActionPerformed

private void jRadioButtonMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem4ActionPerformed
    Object[] forms = new Object[3];
    forms[0] = this;
    forms[1] = dialAtr;
    forms[2] = diaText;
    Temas.cambiarTema(Temas.MOTIF, forms);
}//GEN-LAST:event_jRadioButtonMenuItem4ActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
    chos.setAcceptAllFileFilterUsed(false);
    chos.addChoosableFileFilter(new FileFilter(){
        @Override
            public boolean accept(File f) {
                if(f.getName().contains("xml") || f.isDirectory()){
                    return true;
                }else return false;
            }

            @Override
            public String getDescription() {
                return "xml";
            }});
    
    chos.addChoosableFileFilter(new FileFilter(){
        @Override
            public boolean accept(File f) {
                if(f.getName().contains("png") || f.isDirectory()){
                    return true;
                }else return false;
            }

            @Override
            public String getDescription() {
                return "png";
            }});
    
    chos.addChoosableFileFilter(new FileFilter(){
        @Override
            public boolean accept(File f) {
                if(f.getName().contains("gif") || f.isDirectory()){
                    return true;
                }else return false;
            }

            @Override
            public String getDescription() {
                return "gif";
            }});
    
    int sel = chos.showOpenDialog(null);
    
    if(sel == JFileChooser.APPROVE_OPTION){
        System.out.println(chos.getSelectedFile().getName());
    }
}//GEN-LAST:event_jButton5ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if (info.getName().contains("w")) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(Aplicacion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(Aplicacion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(Aplicacion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(Aplicacion.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                    try {
                        new Aplicacion().setVisible(true);
                    } catch (ParserConfigurationException ex) {
                        Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (SAXException ex) {
                        Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (OpcionDeProcesamientoException ex) {
                        Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
                    }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTree arbol;
    private javax.swing.JTree arbolTags;
    private javax.swing.JTree arbolTexto;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JFileChooser chos;
    private javax.swing.JDialog diaText;
    private javax.swing.JDialog dialAtr;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem2;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem3;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JMenuItem menuItemAcercaDe;
    private javax.swing.JTextField nombre;
    private javax.swing.JCheckBox opAuto;
    private javax.swing.JPanel panelText;
    private javax.swing.JTextField tagText;
    private javax.swing.JCheckBox tagVacio;
    private javax.swing.JTextField texto;
    private javax.swing.JTextField valor;
    // End of variables declaration//GEN-END:variables
//    private static void imprimirInformacion(Tag tagRaiz, javax.swing.tree.DefaultMutableTreeNode raiz) {
//        DefaultMutableTreeNode nodo = new DefaultMutableTreeNode(tagRaiz);
//        if(tagRaiz.isValorNormal()){
//            DefaultMutableTreeNode v = new DefaultMutableTreeNode(tagRaiz.getValor());
//            if(!tagRaiz.getValor().equalsIgnoreCase(""))
//                nodo.add(v);
//        }else if(tagRaiz.isValorCData()){
//            DefaultMutableTreeNode v = new DefaultMutableTreeNode(tagRaiz.getValorCdata());
//            if(!tagRaiz.getValorCdata().getValor().equalsIgnoreCase(""))
//                nodo.add(v);
//        }
//        
//        raiz.add(nodo);
//        System.out.println(tagRaiz);
//        if(tagRaiz.isComentario()){
//            System.out.println("Comentario: <"+tagRaiz.getComentario().toString()+">");
//            nodo.add(new DefaultMutableTreeNode(tagRaiz.getComentario()));
//        }
//              
//        for(Atributo atr:tagRaiz.getAtributos()){
//            System.out.println(atr);
//            nodo.add(new DefaultMutableTreeNode(atr));
//        }
//        
//        for(Tag t: tagRaiz.getTagsHijos()){
//            imprimirInformacion(t, nodo);
//        }
////        DefaultMutableTreeNode nodo = new DefaultMutableTreeNode(tagRaiz.getNombre());
////        if(tagRaiz.isValorNormal()){
////            DefaultMutableTreeNode v = new DefaultMutableTreeNode(tagRaiz.getValor());
////            if(!tagRaiz.getValor().equalsIgnoreCase(""))
////                nodo.add(v);
////        }else if(tagRaiz.isValorCData()){
////            DefaultMutableTreeNode v = new DefaultMutableTreeNode(tagRaiz.getValorCdata());
////            if(!tagRaiz.getValorCdata().getValor().equalsIgnoreCase(""))
////                nodo.add(v);
////        }
////        
////        raiz.add(nodo);
////        System.out.println(tagRaiz);
////        if(tagRaiz.isComentario()){
////            System.out.println("Comentario: <"+tagRaiz.getComentario().toString()+">");
////            nodo.add(new DefaultMutableTreeNode("/* "+tagRaiz.getComentario().toString()+" */"));
////        }
////              
////        for(Atributo atr:tagRaiz.getAtributos()){
////            System.out.println(atr);
////            nodo.add(new DefaultMutableTreeNode(atr.getNombre()+" = \""+atr.getValor()+"\""));
////        }
////        
////        for(Tag t: tagRaiz.getTagsHijos()){
////            imprimirInformacion(t, nodo);
////        }
//    }

    private void crearTextArea() {
        RTextScrollPane scroll = new RTextScrollPane(area);
        area.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_XML);
        area.setColumns(20);
        area.setColumns(20);
        area.setTabSize(4);
        area.setMarkOccurrences(true);
        area.setAutoIndentEnabled(true);
        area.setAnimateBracketMatching(true);
        area.addKeyListener(this);
        area.setCodeFoldingEnabled(true);
        panelText.add(scroll);
        DropTarget d = new DropTarget(area, this);
        d.setActive(true);
    }

    private void abrir() {
        if(Abrir.abrir("xml", "Abrir XML", "")){
            area.setText(Abrir.getArchivoComoString());
            cargarXML(new File(Abrir.getRuta()), true);
        }
    }

    public void cargarXML(File f , boolean guardar){
        try {
            ArchivoXML ar = xml.analizador.XML.procesarArchivoXML(f, xml.analizador.XML.OPCION_COMPLEJA);
            //aca se puede cambiar los iconos
            ImageIcon[] iconos = new ImageIcon[4];
            
            iconos[Tag.TAG] = CellRender.crearIcono("/xml/images/16abierto.png");
            iconos[Tag.COMENTARIO] = CellRender.crearIcono("/xml/images/16comentario.png");
            iconos[Tag.ATRIBUTO] = CellRender.crearIcono("/xml/images/16atributo.png");
            iconos[Tag.TEXTO] = CellRender.crearIcono("/xml/images/16texto.png");
            //aca se puede cambiar los iconos
            tagRaiz = ar.getTagRaiz();
            tagRaiz.construirArbol(arbol, iconos, new Color(240,255,255));
            if (guardar){
                this.archivo = f;
                this.setTitle(NOMBRE_SW+" - "+f.getName());
            }
        } catch (OpcionDeProcesamientoException ex) {
            Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
        } catch ( ParserConfigurationException ex) {
//            Mensaje.error(ex.getMessage());
        } catch (SAXException ex){

        }catch(IOException ex){

        }
    }
    
    private boolean guardar() {
        if(archivo == null){
            if(Guardar.guardarComo("archivo", "xml", "Guardar XML", "/")){
                try {
                    Archivo.escribirEnAchivo(Guardar.getRuta(), area.getText(), true, false);
                    archivo = new File(Guardar.getRuta());
                    this.setTitle(NOMBRE_SW+" - "+archivo.getName());
                } catch (IOException ex) {
                    Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
                    return false;
                }
                return true;
            }else return false;
        }else{
            try {
                Archivo.escribirEnAchivo(archivo.getAbsolutePath(), area.getText(), true, false);
                return true;
            } catch (IOException ex) {
                Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }
    }

    private void refreshBoton() {
        String text = area.getText();
        if (guardar()) {
            try {
                Archivo.escribirEnAchivo(archivo.getAbsolutePath(), text, true, false);
                cargarXML(archivo, true);
            } catch (IOException ex) {
                Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else{
            try {
                Archivo.escribirEnAchivo("temp.xml", text, true, false);
                cargarXML(new File("temp.xml"), false);
            } catch (IOException ex) {
                Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void refresh(){
        String text = area.getText();
        if (archivo == null) {
            try {
                Archivo.escribirEnAchivo("temp.xml", text, true, false);
                cargarXML(new File("temp.xml"), false);
            } catch (IOException ex) {
                Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
            } finally{
            }
        } else {
            try {
                Archivo.escribirEnAchivo(archivo.getAbsolutePath(), text, true, false);
                cargarXML(archivo, true);
            } catch (IOException ex) {
                Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    @Override
    public void keyTyped(KeyEvent e) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void keyPressed(KeyEvent e) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(opAuto.isSelected()){
            refresh();
        }
        
    }

    private void llenarComboConTags(Tag raiz, List nombres) {
        nombres.add(raiz);
        for(Tag t: raiz.getTagsHijos()){
            llenarComboConTags(t, nombres);
        }
    }

    private void refreshInverso() {
        try {
            String text;
            if (archivo == null) {//no lo ha guardado
                xml.analizador.XML.crearArchivoXML(tagRaiz, new File("temp.xml"));
                text = Archivo.leerArchivo(new File("temp.xml").getAbsolutePath());
                area.setText(text);
                cargarXML(new File("temp.xml"), false);
            } else {
                xml.analizador.XML.crearArchivoXML(tagRaiz, archivo);
                text = Archivo.leerArchivo(archivo.getAbsolutePath());
                area.setText(text);
                cargarXML(archivo, true);
            }
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        } catch (TransformerConfigurationException ex) {
            ex.printStackTrace();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (TransformerException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void dragEnter(DropTargetDragEvent dtde) {
        area.setBackground(new Color(186, 228, 255));
    }

    @Override
    public void dragOver(DropTargetDragEvent dtde) {
        
    }

    @Override
    public void dropActionChanged(DropTargetDragEvent dtde) {
        
    }

    @Override
    public void dragExit(DropTargetEvent dte) {
        area.setBackground(Color.white);
    }

    @Override
    public void drop(DropTargetDropEvent evento) {
        area.setBackground(Color.white);
         evento.acceptDrop(evento.getDropAction());
         Transferable tr = evento.getTransferable();
         if(tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)){
            try {
                Object o = tr.getTransferData(DataFlavor.javaFileListFlavor);
                if(o instanceof List){
                    List<File> lista = (List)o;
                    File archivoXML = null;
                    for(File f: lista){
                        if(f.getName().contains("xml")){
                            archivoXML = f;
                            break;
                        }
                    }
                    if(archivoXML != null){
                        final File arXML = archivoXML;
                        new Thread(new Runnable() {

                            @Override
                            public void run() {
                                try {
                                    String ar = Archivo.leerArchivo(arXML.getAbsolutePath());
                                    area.setText(ar);
                                    archivo = arXML;
                                    refresh();
                                } catch (IOException ex) {
                                }
                            }
                        }).start();
                        
                    }
                }
            } catch (UnsupportedFlavorException ex) {
            } catch (IOException ex) {
            }
         }
    }
    
    private void initAcercaDe() {
        AcercaDe ad = new AcercaDe(new DatosAcercaDe() {
            //NOMBRE_SW, "Programa práctico para visualizar archivos XML de forma gráfica", "Patricio Pérez Pinto", "2012", "Patricio Pérez Pinto", GPL.AMBAS_VERSIONES
            @Override
            public String getNombreDelPrograma() {
                return NOMBRE_SW;
            }

            @Override
            public String getdescripcionCortaDelPrograma() {
                return "Programa práctico para visualizar archivos XML de forma gráfica";
            }

            @Override
            public String getAutor() {
                return "Patricio Pérez Pinto";
            }

            @Override
            public String getAno() {
                return "2012";
            }

            @Override
            public String getEmailDeContacto() {
                return "contacto@pperezp.com";
            }

            @Override
            public int getIdiomaLicencia() {
                return GPL.AMBAS_VERSIONES;
            }
        });
        ad.addComponent(this.menuItemAcercaDe);
    }
}
